package com.event.controller;

import com.event.interceptors.LoginInterceptor;
import com.event.pojo.Result;
import com.event.pojo.User;
import com.event.service.UserService;
import com.event.utils.JwtUtil;
import com.event.utils.Md5Util;
import com.event.utils.ThreadLocalUtil;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;


    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String userName, @Pattern(regexp = "^\\S{5,16}$")String password){
        User u = userService.findByUserName(userName);
        if(u == null){
            userService.register(userName, password);
            return Result.success("注册成功");
        }else{
            return Result.error("用户名已被占用4444");
        }

    }

    @RequestMapping("/login")
    public Result login(@Pattern(regexp = "^\\S{5,16}$")String userName, @Pattern(regexp = "^\\S{5,16}$")String password){
        User user = userService.findByUserName(userName);
        if(user == null){
            return Result.error("登录失败");
        }

        if (Md5Util.getMD5String(password).equals(user.getPassword())) {
            Map<String,Object> map = new HashMap<>();
            map.put("userName",user.getUsername());
            map.put("id",user.getId());
            String token = JwtUtil.genToken(map);
            return Result.success(token);
        }else {
            return Result.error("登录失败");
        }


    }

    @GetMapping("/userInfo")
    public Result<User> userInfo(){
        Map<String,Object> user = ThreadLocalUtil.get();
        String userName = user.get("userName").toString();
        User u = userService.findByUserName(userName);
        return Result.success(u);
    }

    @PutMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody  @Valid   User user){
        try {
            userService.update(user);
            return Result.success("修改成功");
        } catch (Exception e) {
           return Result.error("修改失败");
        }
    }

    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam @URL String avatarUrl){
        try {
            userService.updateAvatar(avatarUrl);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.error("更新失败");
        }

    }

    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,String> params){
        try {
            //校验参数
            String old_pwd = params.get("old_pwd");
            String new_pwd = params.get("new_pwd");
            String re_pwd = params.get("re_pwd");

            if(!StringUtils.hasLength(old_pwd)||!StringUtils.hasLength(new_pwd)||!StringUtils.hasLength(re_pwd)){
                 return Result.error("密码不能为空");
            }

            //原密码是否正确
            Map<String,Object> logingUser = ThreadLocalUtil.get();
            String userName = logingUser.get("userName").toString();
            User user = userService.findByUserName(userName);
            String pwd = user.getPassword();
            if(StringUtils.hasLength(pwd)){
                if(!pwd.equals(Md5Util.getMD5String(old_pwd))){
                    return Result.error("原密码不正确");
                }
            }
            //校验新密码和确认密码是否一致
            if(!new_pwd.equals(re_pwd)){
                return Result.error("两次填写的密码不一致");
            }
            userService.updatePwd(new_pwd);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.error("更新失败");
        }
    }
}
